package com.zhouchb.blog.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhouchb.blog.bean.Blog;
import com.zhouchb.blog.bean.Comment;
import com.zhouchb.blog.bean.Tag;
import com.zhouchb.blog.bean.Type;
import com.zhouchb.blog.service.BlogService;
import com.zhouchb.blog.service.CommentService;
import com.zhouchb.blog.service.TagsService;
import com.zhouchb.blog.service.TypesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Zhouchb
 * @Create 2021-09-2021/9/2 :10:38
 * @Description
 */
@Controller
@Slf4j
public class IndexController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypesService typesService;
    @Autowired
    private TagsService tagesService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/")
    public String toIndex(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        List<Blog> listBlogRecommend = blogService.listRecommend(6);
        List<Type> types = typesService.listTypeNum(6);
        List<Tag> tags = tagesService.listOrderCount(6);
        PageHelper.startPage(pn, 6);
        List<Blog> blogList = blogService.getAllBlogDao();
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList, 5);
        model.addAttribute("blogPageInfo", blogPageInfo);
        model.addAttribute("listBlogRecommend", listBlogRecommend);
        model.addAttribute("types", types);
        model.addAttribute("tags", tags);
        return "index";
    }

    @GetMapping("/blog/{id}")
    public String toBlog(@PathVariable("id")Long id, Model model) {
        Blog blog = blogService.getBlogByIdConvert(id);
        log.info("blog的id:{}",blog.getId());
        blogService.updateViews(blog.getId());
        List<Tag> tags = tagesService.listTagbyBlogIdService(blog.getId());
        List<Comment> comments = commentService.listCommentByBlogIdService(blog.getId());
       // System.out.println(comments);
        model.addAttribute("comments",comments);
        model.addAttribute("blog",blog);
        model.addAttribute("tags",tags);
        return "blog";
    }

 /*   @GetMapping("/types")
    public String toAbout() {
        return "types";
    }*/

    /*搜索*/

    @PostMapping("/search")
    public String search(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                         @RequestParam("query")String query, Model model) {
        log.info("pn:{},query:{}",pn,query);
        PageHelper.startPage(pn,6);
        List<Blog> searchBlogList = blogService.searchBlogService(query);
        PageInfo<Blog> blogPageInfo = new PageInfo<>(searchBlogList, 5);
        model.addAttribute("searchblogPageInfo",blogPageInfo);
        model.addAttribute("query",query);
        return "search";
    }

    @GetMapping("/footer/newBlow")
    public String footerNewBlow(Model model){
        List<Blog> blogList = blogService.listRecommend(3);
        model.addAttribute("newBlows",blogList);
        return "_fragments :: newblowlist";
    }
}


